package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 购销合同的dubbo服务
 */
@Service
public class ContractServiceImpl implements ContractService {

	@Autowired
	private ContractDao contractDao;

	@Override
	public Contract findById(String id) {
		return contractDao.selectByPrimaryKey(id);
	}


	public void save(Contract contract) {
		contract.setId(UUID.randomUUID().toString());
		//默认属性
		contract.setState(0); // 0:草稿
		contract.setTotalAmount(0.0); //总金额
		contract.setCreateTime(new Date()); //保存购销合同的创建时间    ： 查询列表的时候。可以根据创建时间倒序排序
		contractDao.insertSelective(contract);
	}

	@Override
	public void update(Contract contract) {
		contractDao.updateByPrimaryKeySelective(contract);
	}

	@Override
	public void delete(String id) {
		contractDao.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo findAll(ContractExample example, int page, int size) {
		//分页
		PageHelper.startPage(page,size);
		//查询列表
		List list = contractDao.selectByExample(example);
		//返回PageInfo
		return new PageInfo(list);
	}

	//根据船期查询出货表数据
	public List<ContractProductVo> findByShipTime(String shipTime, String companyId) {
		return contractDao.findByShipTime(shipTime,companyId);
	}

}
